/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.qdu.servlet;

import com.qdu.entity.Favorites;
import com.qdu.entity.User;
import com.qdu.service.FavoritesService;
import com.qdu.service.impl.FavoritesServiceImpl;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Ice
 */
@WebServlet("/atf")
public class AddToFavorites extends HttpServlet {

    FavoritesService favoritesService = new FavoritesServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try {
            req.setCharacterEncoding("utf-8");

            User user = new User();
            HttpSession session = req.getSession();
            user = (User) session.getAttribute("user");
            int uid = user.getId();// 得到该登录用户的id号

            String mbid = req.getParameter("mbid");

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            Date date = new Date();
            String dateStr = sdf.format(date);
            Date mydate = sdf.parse(dateStr);

            Favorites favorites = new Favorites(uid, mbid, mydate);

            int rows = favoritesService.addToFavorites(favorites);

            //生成响应内容，这里直接返回了受影响的行数，用于判断是否插入成功
            resp.getWriter().println(rows);

        } catch (ParseException ex) {
            Logger.getLogger(AddToFavorites.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}
